package com.dcn.sign.util;

import com.dcn.sign.model.SignRecord;
import com.dcn.sign.vo.BgColor;
import com.dcn.sign.vo.DailyRecord;
import com.zj.util.base.eo.DatePattern;
import com.zj.util.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author xi.yang
 * @create 2018-09-16 11:41
 **/
public class DataStrUtil {
    private static final Date START_MONTH = DateUtil.parseDate("2018-08");

    /**
     * 获取所有的月份
     * @return
     */
    public static List<String> getMonthStrs() {
        List<String> monthStrs = new ArrayList<>();
        Date monthDate = START_MONTH;
        while (true) {
            if (new Date().getTime() < monthDate.getTime()) {
                break;
            }
            monthStrs.add(DateUtil.fmtDate(monthDate, DatePattern.yyyyMM));
            monthDate = DateUtil.getNextMonthFirstDate(monthDate);
        }
        return monthStrs;
    }

    public static List<List<DailyRecord>> initDailyReccordData(String month) {
        List<Date> allDates = getDatesByMonth(month);
        List<List<DailyRecord>> dailyRecords = new ArrayList<>();
        List<DailyRecord> list = new ArrayList<>();
//        补全前面一周的数据
        final Date firstDate = allDates.get(0);
        int weekIndex = DateUtil.getWeek(firstDate).getId();
        for (int i = 1; i < weekIndex; i++) {
            final Date date = DateUtil.addDays(firstDate, i - weekIndex);
            list.add(new DailyRecord(date));
        }
//        转换数据
        for (Date date : allDates) {
            DailyRecord dailyRecord = new DailyRecord(date);
            list.add(dailyRecord);
            if (list.size() == 7) {
                dailyRecords.add(list);
                list = new ArrayList<>();
            }
            continue;
        }
//        补全后面一周的数据
        final Date lastDate = allDates.get(allDates.size() - 1);
        weekIndex = DateUtil.getWeek(lastDate).getId();
        for (int i = 1; i < 8-weekIndex; i++) {
            DailyRecord dailyRecord = new DailyRecord(DateUtil.addDays(lastDate,i));
            list.add(dailyRecord);
        }
        if (list.size() > 0) {
            dailyRecords.add(list);
        }
        return dailyRecords;
    }

    public static Date getMonthEndDay(String month) {
        Date curMonth = DateUtil.parseDate(month);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(curMonth);
        calendar.set(Calendar.DAY_OF_MONTH, 25);
        return calendar.getTime();
    }

    public static Date getMonthStartDay(String month) {
        Date curMonth = DateUtil.parseDate(month);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(curMonth);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 26);
        return calendar.getTime();
    }

    /**
     * 获取传入月份
     * @param month
     * @return
     */
    public static List<Date> getDatesByMonth(String month) {
        List<Date> dateList = new ArrayList<>();
        Date curMonth = DateUtil.parseDate(month);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(curMonth);
        calendar.set(Calendar.DAY_OF_MONTH, 25);
        final Date end = calendar.getTime();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 25);
        dateList.add(calendar.getTime());
        while (true) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
            if (calendar.getTime().getTime() > end.getTime()) {
                break;
            }
            dateList.add(calendar.getTime());
        }
        return dateList;
    }
}
